(function(){
	var api = 'http://h6.duchengjiu.top/shop/';
	var passWord = document.getElementById('password');
	var userName = document.getElementById('username');
	var nameMsg = document.getElementById('name-msg');
	var psdMsg = document.getElementById('psd-msg');
	var registerBtn = document.getElementById('register');
	//console.log(passWord);
	//console.log(userName);
	//console.log(nameMsg);
	//console.log(psdMsg);
	var lock01 = false;
	var lock02 = false;
	registerBtn.disabled = true;
	
	//失去焦点的时候要做的事
	userName.onblur = function(){
		var userNameV = userName.value;
		//var passWordV = passWord.value;
		var re = /^[a-zA-Z0-9_]{3,20}$/g;
		//判断正则
		//console.log(re.test(userNameV));
		if(!re.test(userNameV)){
			nameMsg.innerHTML = 'X';
			nameMsg.style.color = 'red';
			lock01 = false;
			registerBtn.disabled = true;
			
		}else{
			$.post(api+'api_user.php',{'status':'check','username':userNameV},function(data){
				//console.log(data);
				if(data.code == 0){
					nameMsg.innerHTML = '用户名可用';
					nameMsg.style.color = 'green';
					lock01 = true;
					if(lock01 &&lock02){
						registerBtn.disabled = false;
					};
				}else if(data.code == 2001){
					nameMsg.innerHTML = '用户名已存在';
					nameMsg.style.color = 'red';
					lock01 = false;
					registerBtn.disabled = true;
				};
			});
		};
	};
	
	passWord.onkeyup = function(){
		var passWordV = passWord.value;
		//console.log(passWordV);
		var re = /^[a-z0-9_]{6,20}$/g;

		if(!re.test(passWordV)){
			psdMsg.innerHTML = '请输入6-20位字符';
			psdMsg.style.color = 'red';
			lock02 = false;
			registerBtn.disabled = true;
		}else{
			//console.log(1);
			psdMsg.innerHTML = '密码可用';
			psdMsg.style.color = 'green';
			lock02 = true;
			if(lock01 && lock02){
				registerBtn.disabled = false;
			};
		};
	};
	
	registerBtn.onclick = function(){
		var userNameV = userName.value;
		var passWordV = passWord.value;
		$.post(api+'api_user.php',{'status':'register','username':userNameV,'password':passWordV},function(data){
			console.log(data);
			if(data.code == 0){
				console.log(1);
				userName.innerHTML = '';
				passWord.innerHTML = '';
				alert('注册成功');
				location.href = 'login.html';
			}
		});
	};
})();
//
//
//
//
//
//
//
//
//
//
//
//
//
//hehe